package com.sitanyun.merchant.guide.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.bean.SmsPhoneBean;
import com.sitanyun.merchant.guide.frament.view.activity.SuccessPasswordActivity;
import com.sitanyun.merchant.guide.presenter.impl.SetingPaswdAPresenterImpl;
import com.sitanyun.merchant.guide.presenter.inter.ISetingPaswdAPresenter;
import com.sitanyun.merchant.guide.view.inter.ISetingPaswdAView;
import com.sitanyun.merchant.guide.weiht.PhoneUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置密码
 */
public class SetingPaswdActivity extends BaseActivity implements ISetingPaswdAView {


    @BindView(R.id.setup_paswd)
    EditText setupPaswd;
    @BindView(R.id.setup_msg_paswd)
    TextView fogetPhoneMsgthree;
    @BindView(R.id.setups_paswd)
    EditText setupsPaswd;
    @BindView(R.id.paswd_layout)
    LinearLayout paswdLayout;
    @BindView(R.id.setups_msg_paswd)
    TextView fogetPassMsgthree;
    @BindView(R.id.setup_login)
    TextView setupLogin;
    @BindView(R.id.setup_int)
    TextView setupInt;
    @BindView(R.id.tv_return)
    ImageView tvReturn;
    @BindView(R.id.tv_title_str)
    TextView tvTitleStr;
    @BindView(R.id.image_right_key)
    ImageView imageRightKey;
    @BindView(R.id.tv_right_key)
    TextView tvRightKey;
    @BindView(R.id.ff_main_layout)
    FrameLayout ffMainLayout;
    private ISetingPaswdAPresenter mISetingPaswdAPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_seting_paswd;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        setFindViewById(true);
        tvTitleStr.setText("设置密码");
        tvReturn.setVisibility(View.GONE);
    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mISetingPaswdAPresenter = new SetingPaswdAPresenterImpl(this);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.setup_login, R.id.setup_int})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setup_login:
                String threephone = setupPaswd.getText().toString().trim();
                String threepaswd = setupsPaswd.getText().toString().trim();
                if (threephone.isEmpty()) {
                    fogetPhoneMsgthree.setText("请输入密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (threepaswd.isEmpty()) {
                    fogetPassMsgthree.setText("请再次输入密码");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (!StringUtils.equals(threephone, threepaswd)) {
                    fogetPassMsgthree.setText("两次密码输入不一致");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }

                if (threephone.length() < 6) {
                    fogetPhoneMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    fogetPassMsgthree.setVisibility(View.GONE);
                    return;
                }
                if (threepaswd.length() < 6) {
                    fogetPassMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (!PhoneUtils.isLetterDigit(threephone) || !PhoneUtils.isLetterDigit(threepaswd)) {
                    fogetPhoneMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    fogetPassMsgthree.setVisibility(View.GONE);
                    return;
                }

                mISetingPaswdAPresenter.getingpswd(SharedPreferenceUtil.getStringData("userid"),
                        threepaswd, getIntent().getStringExtra("userId"),
                        getIntent().getStringExtra("phone"));
                break;
            case R.id.setup_int:
                startActivity(new Intent(SetingPaswdActivity.this, MainActivity.class)
                        .putExtra("cid","null"));
               finish();
                break;
        }
    }

    @Override
    public <T> T request(int requestFlag) {
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {
        if (responseFlag == ISetingPaswdAView.RESPONSE_ONE) {
            SmsPhoneBean baseEntity = (SmsPhoneBean) response;
            if (baseEntity.getCode()==0) {
                startActivity(new Intent(SetingPaswdActivity.this, SuccessPasswordActivity.class));
                SharedPreferenceUtil.SaveData("pswd", setupsPaswd.getText().toString().trim());
                finish();

            } else {
                fogetPassMsgthree.setVisibility(View.VISIBLE);
                fogetPassMsgthree.setText(baseEntity.getMsg()+"");
            }

        }

    }
}
